package com.sendi.system.util.common;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import com.sendi.sql.FieldInf;

 

/*
 * 反射的一些工具类
 */
public class ReflectUtil {
	
	
	public static void copyFiledValueFromOtherobj(Object surceobj,Object descobj,String fildname[],String fieldtype[])
	{
		int len=fildname.length;
		if(len!=fieldtype.length)
		{
			try {
				throw new Exception("字段名称数组跟字段类型数据长度不一致！");
			} catch (Exception e) {
			 
				e.printStackTrace();
			}
		}
		else
		{
			for(int i=0;i<len;i++)
			{
				Object surceobjvalue=getReflectFieldValue(surceobj,fildname[i]);
				setFieldValueByReflect(descobj,fildname[i],fieldtype[i],surceobjvalue);
			}
		}
		
	}
	
	 
	public static void copyFiledValueFromOtherobj(Object surceobj,Object descobj,ArrayList<FieldInf> Filedobj)
	{
		try
		{
		 	for(FieldInf F:Filedobj)
			{
				if(F!=null)
				{
					Object surceobjvalue=getReflectFieldValue(surceobj,F.getFieldname());
				 
					setFieldValueByReflect(descobj,F.getFieldname(),F.getFieldType(),surceobjvalue);
				}
				else
				{
					//System.out.println("出现一个为空的字段信息，请检查...............");
				}
	 			
			}
		}catch (Exception e) {
			 e.printStackTrace();
		}
	 		
 	}
	public static Object getReflectFieldValue(Object o1,String fieldname)
	{
		Field f=null;
	
		Object returnobj=null;
		try {
			try
			{
				f=o1.getClass().getDeclaredField(fieldname);
			}catch (Exception e) {
				 f=o1.getClass().getSuperclass().getDeclaredField(fieldname);
			}
			
			
			f.setAccessible(true);
			returnobj=f.get(o1);
		} catch (IllegalArgumentException e) {
	 		e.printStackTrace();
		} catch (IllegalAccessException e) {
		 	e.printStackTrace();
		} catch (SecurityException e) {
		 
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
		 
			e.printStackTrace();
		}
		return returnobj;
	}
	
	
	public static Object getReflectFieldValue(Object o1,Field f)
	{
		 
	
		Object returnobj=null;
		try {
			if(!f.isAccessible())
			{
				f.setAccessible(true);	
			}
	 		returnobj=f.get(o1);
		} catch (IllegalArgumentException e) {
	 		e.printStackTrace();
		} catch (IllegalAccessException e) {
		 	e.printStackTrace();
		} catch (SecurityException e) {
		 
			e.printStackTrace();
		} 
		return returnobj;
	}
	
	
	public static void InvokeMethdToRun(Object o,String methname)
	{
		Method method=null;
		try {
			method=o.getClass().getDeclaredMethod(methname);
			method.setAccessible(true);
			method.invoke(o);
		} catch (SecurityException e) {
		 
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			 
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
		 
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			 
			e.printStackTrace();
		} catch (InvocationTargetException e) {
		 
			e.printStackTrace();
		}
	}
	
	public static void InvokeMethdToRun(Object o,Method method)
	{
	 
		try {
			if(!method.isAccessible())
			{
				method.setAccessible(true);
			}
 			method.invoke(o);
		} catch (SecurityException e) {
		 
			e.printStackTrace();
		}  catch (IllegalArgumentException e) {
		 
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			 
			e.printStackTrace();
		} catch (InvocationTargetException e) {
		 
			e.printStackTrace();
		}
	}
	
	
	public static void setFieldValueByReflect(Object o,String fildname,String fieldtype,Object filevalue)
	{
		Field f=null;
	 	try {
			try
			{
				 f=o.getClass().getDeclaredField(fildname);
				 
			}catch (Exception e) {
				 if(f==null)
						f=o.getClass().getSuperclass().getDeclaredField(fildname);
			}
	 		f.setAccessible(true);
			if(fieldtype.equalsIgnoreCase("INT"))
			{
				 f.setInt(o, Integer.parseInt(filevalue.toString()));
			}
			else if(fieldtype.equalsIgnoreCase("LONG"))
			{
				 f.setLong(o, Long.parseLong(filevalue.toString()));
			}
			else if(fieldtype.equalsIgnoreCase("FLOAT"))
			{
				f.setFloat(o, Float.parseFloat(filevalue.toString()));
			}
			else if(fieldtype.equalsIgnoreCase("DOUBLE"))
			{
				 f.setDouble(o, Double.parseDouble(filevalue.toString()));
			}
			else
			{
				f.set(o, filevalue);
			}
			
		 
		} catch (Exception e) {
		 
			e.printStackTrace();
			System.out.println("error:反射调用字段设值时出错了"+fildname+e.getLocalizedMessage());
		}
 	}
	public static void setFieldValueByReflect(Object o,Field f,String fieldtype,Object filevalue)
	{
		 try
		 {
			if(!f.isAccessible())
			{
				f.setAccessible(true);	
			}
			if(fieldtype.equalsIgnoreCase("INT"))
			{
				 f.setInt(o, Integer.parseInt(filevalue.toString()));
			}
			else if(fieldtype.equalsIgnoreCase("LONG"))
			{
				 f.setLong(o, Long.parseLong(filevalue.toString()));
			}
			else if(fieldtype.equalsIgnoreCase("FLOAT"))
			{
				f.setFloat(o, Float.parseFloat(filevalue.toString()));
			}
			else if(fieldtype.equalsIgnoreCase("DOUBLE"))
			{
				 f.setDouble(o, Double.parseDouble(filevalue.toString()));
			}
			else
			{
				f.set(o, filevalue);
			}
			
		 
		} catch (Exception e) {
		 
			//e.printStackTrace();
			System.out.println("error:反射调用字段设值时出错了："+e.getLocalizedMessage());
		}
 	}
	
	

}
